<?php
/**
 * User: zj
 * Date: 2017/5/10
 * Time: 17:28
 * Progress:0%
 */

namespace logic\admin\index;


use model\Account;
use model\AccountWechat;

use think\Db;

class LAccount
{


    public function index_data($param){
        $model = new Account();
        $where=array();

        $keywords = $param['search'];
        if (!empty($keywords)) {
            $where['a.name|nick|mobile|email|wechat'] = array('like', "%{$keywords}%"); //查询条件
        }

        $status = $param['status'];
        if ($status > -1) {
            $where['a.flag_lock'] = $status;
        }

        $where['a.flag_delete'] = 0;

        if(!empty($param['begindate'])){
            $begin_date = strtotime($param['begindate']);
        }else{
            $begin_date = '';
        }
        if(!empty($param['enddate'])){
            $end_date = strtotime('+1 day',strtotime($param['enddate']));
        }else{
            $end_date = '';
        }

        if (!empty($begin_date) && !empty($end_date)) {
            $where['a.reg_time'] = array('between', "{$begin_date},{$end_date}");
        }else if(!empty($begin_date) && empty($end_date)){
            $where['a.reg_time'] = array('egt', $begin_date);
        }else if(empty($begin_date) && !empty($end_date)){
            $where['a.reg_time'] = array('elt', $end_date);
        }
        //配送员
//        $where['ar.role_id']=array('neq',2);
        $total=$model->alias('a')
            ->where($where)
            ->count(); //记录总数

        $page_size =$param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['ordinal'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['a.reg_time'] = 'desc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束


        $rows = $model->alias('a')
            ->where($where)
            ->field("a.*,(select GROUP_CONCAT(concat_ws('@@',abr.name) SEPARATOR '&&') from ks_account_role ar left join ks_account_base_role abr on abr.id=ar.role_id where ar.account_id=a.id  and ar.flag_delete=0) as role_name")
            ->limit($start, $page_size)
            ->order($sort)
            ->select();

        $array=array(
            'total'=>$total,
            'rows'=>$rows
        );

        return $array;

    }

    //会员详情
    public function detail_data($id){
        $model = new Account();
        $data = $model->alias('a')
            ->join('account ac','ac.id = a.parent_id','left')
            ->join('account_wechat aw','a.id = aw.account_id','left')
            ->field('a.*,ac.name as parent_name,aw.openid,aw.nickname,aw.sex,aw.province,aw.city,aw.country,aw.privilege,aw.unionid')
            ->where(['a.id' => $id])
            ->find();
        return $data;
    }




    //会员信息更新
    public function update_data($id,$param){
        $model = new Account();

        $data['nick'] = $param['nick'];
        $data['name'] = $param['name'];
        $data['mobile'] = $param['mobile'];
        $data['email'] = $param['email'];
        $data['wechat'] = $param['wechat'];
        $data['flag_lock'] = $param['flag_lock'];
        $data['money'] = $param['money'];
        $a = $model->where(['id'=>$id])->update($data);


        if($a!==false){
            return array(
                'success'=>true,
                'message'=>'修改成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()
            );
        }
    }

    //会员删除
    public function delete_data($id)
    {
        $model = new Account();
        $data['flag_delete'] = 1;
        $res = $model->where(['id' => $id])->update($data);
        $res1=DB::name('account_role')->where('account_id',$id)->delete();
        if ($res && $res1) {
            return array(
                'success' => true,
                'message' => '删除成功'
            );
        } else {
            return array(
                'success' => false,
                'message' => $model->getError()

            );
        }
    }

    public  function  update_role($id,$role_id){
        $model = new Account();
        $data['account_id'] = $id;
        $data['role_id'] = $role_id;
        $data['update_time']=time();
        $data['add_time']=time();
        $count=DB::name('account_role')->where(['account_id'=>$id,'role_id'=>$role_id])->find();
        if(!$count){
            $res=DB::name('account_role')->where('id',$id)->insert($data);
            if ($res) {
                return array(
                    'success' => true,
                    'message' => '成功'
                );
            } else {
                return array(
                    'success' => false,
                    'message' => $model->getError()
                );
            }
        }else{
            return array(
                'success' => false,
                'message' => '该帐号已经是配送员'
            );
        }

    }


}